//
//  ViewController.m
//  QRCodeCreated
//
//  Created by huanghuan on 16/9/28.
//  Copyright © 2016年 huanghuan. All rights reserved.
//

#import "ViewController.h"
#import "QRImageUtil.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)createQRCode:(id)sender {
    [_userPwdTextFiled resignFirstResponder];
    [_userNameTextField resignFirstResponder];
    [_wifiNameTextField resignFirstResponder];
    [_WifiPwdTextField resignFirstResponder];
    
    NSString *ssid = _wifiNameTextField.text;
    NSString *psw = _WifiPwdTextField.text;
    NSString *username = _userNameTextField.text;
    NSString *password = _userPwdTextFiled.text;
    
    NSString *message = nil;
    if (ssid.length == 0) {
        message = @"请输入wifi名称";
    }else if(psw.length == 0){
        message = @"请输入wifi密码";
    }else if(username.length ==0){
        message = @"请输入用户名";
    }else if(password.length == 0){
        message = @"请输入用户密码";
    }
    if (message != nil) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:_company.text forKey:@"company"];
    [dict setObject:psw forKey:@"psw"];
    [dict setObject:ssid forKey:@"ssid"];
    
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    [dataDict setObject:username forKey:@"username"];
    [dataDict setObject:password forKey:@"password"];
    [dict setObject:dataDict forKey:@"data"];
    
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *qrStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    UIImage *image = [QRImageUtil qrImageForString:qrStr imageSize:200 logoImageSize:10];
    [_QRCodeImageView setImage:image];
    
}
@end
